﻿using MS_Entity;
using System;
using System.Activities;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MS_UI.HardwareConfig
{
    public partial class NewVar : Form
    {
        public NewVar()
        {
            InitializeComponent();
            this.Load += InitParams();
        }

        private EventHandler InitParams()
        {
            return (s, e) =>
            {
                this.comboBox1.Items.AddRange(new string[]
                {
                    "Bool", "Signed", "Unsigned", "Hex","Long","Long Inverse",
                    "Float","Float Inverse", "Double","Double Inverse"
                });
                this.comboBox1.SelectedIndex = 6;
                this.comboBox2.Items.AddRange(new string[]
                {
                    "01 Coil Status(0x)", "02 Input Status(1x)",
                    "03 Holding Register(4x)","04 Input Register(3x)"
                });
                this.comboBox2.SelectedIndex = 2;
                CheckVar();
            };
        }
        //变量实例
        public Variable_Modus variables = new Variable_Modus();
        public VarAlarm_Modbus varAlarms = new VarAlarm_Modbus();
        private void button1_Click(object sender, EventArgs e)
        {
           
            //变量名称
            variables.VarName = this.textBox1.Text.Trim();
            //地址
            variables.Address = string.IsNullOrEmpty(this.textBox2.Text.Trim()) 
                ? "0" 
                : this.textBox2.Text.Trim();
            //注释与说明
            variables.Note = this.textBox3.Text.Trim();
            //数据类型
            variables.DataType = this.comboBox1.Text.Trim();
            //存储区域
            variables.StoreArea = this.comboBox2.Text.Trim();
            //报警
            variables.IsAlarm  = this.checkBox1.Checked ;
            //归档
            variables.IsFiling = this.checkBox2.Checked ;
            if (this.lolo.Checked)
            {
                varAlarms.VarAlarm.Add(new Alarm()
                {
                   Priority = int.Parse(this.lo2.Text.Trim()),
                   AlarmType = "LoLo",
                   AlarmValue = int.Parse(lo1.Text.Trim()),
                   Note = this.lo3.Text.Trim(),
                });
            }
            if (this.hihi.Checked)
            {
                varAlarms.VarAlarm.Add(new Alarm()
                {
                    Priority = int.Parse(this.hihi2.Text.Trim()),
                    AlarmType = "Hihi",
                    AlarmValue = int.Parse(hihi1.Text.Trim()),
                    Note = this.hihi3.Text.Trim(),
                });
            }
            if (this.low.Checked)
            {
                varAlarms.VarAlarm.Add(new Alarm()
                {
                    Priority = int.Parse(this.low2.Text.Trim()),
                    AlarmType = "Low",
                    AlarmValue = int.Parse(low1.Text.Trim()),
                    Note = this.low3.Text.Trim(),
                });
            }
            if (this.high.Checked)
            {
                varAlarms.VarAlarm.Add(new Alarm()
                {
                    Priority = int.Parse(this.high2.Text.Trim()),
                    AlarmType = "High",
                    AlarmValue = int.Parse(high1.Text.Trim()),
                    Note = this.high3.Text.Trim(),
                });
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
#region  CheckVar 检查变量，如果一级报警开，则二级报警开，如果二级报警开，则三级开
        private void CheckVar()
        {
            if (this.checkBox1.Checked)
            {
                this.low.Enabled = true;
                this.high.Enabled = true;
                this.hihi.Enabled = true;
                this.lolo.Enabled = true;
            }
            else
            {
                this.low.Enabled = false;
                this.high.Enabled = false;
                this.hihi.Enabled = false;
                this.lolo.Enabled = false;
            }
            if (this.low.Checked)
            {
                this.low1.Enabled = true;
                this.low2.Enabled = true;
                this.low3.Enabled = true;
            }
            else
            {
                this.low1.Enabled = false;
                this.low2.Enabled = false;
                this.low3.Enabled = false;
            }
            if (this.lolo.Checked)
            {
                this.lo1.Enabled = true;
                this.lo2.Enabled = true;
                this.lo3.Enabled = true;
            }
            else
            {
                this.lo1.Enabled = false;
                this.lo2.Enabled = false;
                this.lo3.Enabled = false;
            }
            if (this.high.Checked)
            {
                this.high1.Enabled = true;
                this.high2.Enabled = true;
                this.high3.Enabled = true;
            }
            else
            {
                this.high1.Enabled = false;
                this.high2.Enabled = false;
                this.high3.Enabled = false;
            }
            if (this.hihi.Checked)
            {
                this.hihi1.Enabled = true;
                this.hihi2.Enabled = true;
                this.hihi3.Enabled = true;
            }
            else
            {
                this.hihi1.Enabled = false;
                this.hihi2.Enabled = false;
                this.hihi3.Enabled = false;
            }
        }
#endregion
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            CheckVar();
        }

        private void lolo_CheckedChanged(object sender, EventArgs e)
        {
            CheckVar();
        }

        private void low_CheckedChanged(object sender, EventArgs e)
        {
            CheckVar();
        }

        private void hihi_CheckedChanged(object sender, EventArgs e)
        {
            CheckVar();
        }

        private void high_CheckedChanged(object sender, EventArgs e)
        {
            CheckVar();
        }
    }
}
